<template>
  <div class="addressEdit">
    <van-nav-bar
      :title="aid ? '编辑地址' : '新增地址'"
      left-text="返回"
      left-arrow
      @click-left="back"
    />
    <Bg>
      <van-address-edit
        :area-list="areaList"
        show-postal
        :show-delete="!!aid"
        show-set-default
        show-search-result
        :detail-maxlength="30"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="originAddressInfo"
        @save="judge"
        @delete="deletes"
      />
    </Bg>
  </div>
</template>

<script>
import Bg from "../components/bg";
import areaList from "../assets/js/area";
import "../assets/less/addressEdit.less";
export default {
  name: "AddressEdit",
  data() {
    return {
      //省市区数据
      areaList,

      //编辑地址的aid
      aid: "",

      //编辑地址初始值
      originAddressInfo: {},
    };
  },
  components: {
    Bg,
  },
  created() {
    //截取路由参数
    this.aid = this.$route.params.aid;
    // console.log("this.aid =====> ", this.aid);
    //如果存在aid,则根据aid查询地址数据
    if (this.aid) {
      this.getAddressDataByAid();
    }
  },
  methods: {
    //返回上一级
    back() {
      this.$router.go(-1);
    },
    //保存地址
    save(value) {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        console.log("浏览器token不存在");
        return this.$router.push({ name: "Login" });
      }

      //复制value
      let data = {
        ...value,
        tokenString,
        appkey: this.appkey,
      };
      data.isDefault = Number(data.isDefault);
      delete data.country;

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        //请求方式
        method: "POST",
        url: this.baseUrl + "/addAddress",
        data,
      })
        .then((res) => {
          console.log("res save ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 9000) {
            //如果添加成功, 跳回地址列表组件
            this.$router.push({ name: "AddressList" });
          }

          this.$toast({
            message: res.data.msg,
            forbidClick: true,
            duration: 3000,
          });
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    //根据aid获取地址数据
    getAddressDataByAid() {
      //查询地址数据
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        console.log("浏览器token不存在");
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        //请求方式
        method: "GET",
        url: this.baseUrl + "/findAddressByAid",
        params: {
          appkey: this.appkey,
          tokenString,
          aid: this.aid,
        },
      })
        .then((res) => {
          // console.log("res getAddressDataByAid ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 40000) {
            let data = res.data.result[0];
            data.isDefault = !!data.isDefault;

            // res.data.result.forEach(v => {
            //   v.isDefault = Boolean(v.isDefault);
            //   // v.isDefault = !!v.isDefault;
            // })

            this.originAddressInfo = data;
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    //编辑地址
    Editaddress(value) {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        console.log("浏览器token不存在");
        return this.$router.push({ name: "Login" });
      }

      //复制value
      let data = {
        ...value,
        tokenString,
        appkey: this.appkey,
      };
      data.isDefault = Number(data.isDefault);
      delete data.country;

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        //请求方式
        method: "POST",
        url: this.baseUrl + "/editAddress",
        data,
      })
        .then((res) => {
          // console.log("res save ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 30000) {
            //如果编辑地址成功, 跳回地址列表组件
            this.$router.push({ name: "AddressList" });
          }

          this.$toast({
            message: res.data.msg,
            forbidClick: true,
            duration: 3000,
          });
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    //判断是保存还是编辑地址
    judge(value) {
      if (this.aid) {
        this.Editaddress(value);
      } else {
        this.save(value);
      }
    },
    //确认删除
    deletes() {
      let tokenString = this.$cookies.get("tokenString");
      // console.log("tokenString ==> ", tokenString);

      if (!tokenString) {
        console.log("浏览器token不存在");
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      this.axios({
        method: "POST",
        url: this.baseUrl + "/deleteAddress",
        data: {
          appkey: this.appkey,
          tokenString,
          aid: this.aid,
        },
      })
        .then((res) => {
          // console.log("res getShopbagData ==>", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            // return this.$router.push({ name: "Login" });
          }

          if (res.data.code == 10000) {
            //如果删除成功, 跳回地址列表组件
            this.$router.push({ name: "AddressList" });
          }
          this.$toast({
            message: res.data.msg,
            forbidClick: true,
            duration: 1000,
          });
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==>", err);
        });
    },
  },
};
</script>
